var addNum = 0,nowNum = 0,myheight,itemHeight;
(function(){
	getChatJson();
	move();
})();

var addNum = 0,nowNum = 0,first = true,itemHeight = 0,is_animat = true,isSend = false,first = true;
var tempChat = [],seeIndex = 0,itemScale;
var is_send = false,wxid = -1,chatMaxNum = 100,now = Date.parse(new Date()),commentMinuteMax = 10,number = 0;
var cmchat = $(".cm-list");
function getChatJson(){
	tempChat = [];
	var jsonurl = "/ws/index.php?g=mobile&m=message&a=getCommentParlour";
	$.getJSON(jsonurl, function(data){
		tempChat = data;
	})
}

function move(){ 
	itemScale = setInterval(function(){
		var chat = tempChat[seeIndex];
		//正常弹幕
		if(chat!=undefined&&chat!=null&&chat!=""){//正常弹幕执行
			if(chat.status=="immediately"){
				var html ="<div class='userliu cm-item ' >" +
								"<div class='toux'><img src='/ws/Mobile/Webroot/gf/images/g4logo.png'></div>" +
									"<div class='nameping'>" +
									"<span>城市客厅用户：<span class='sppin'>"+chat.comment+"</span></span>"+
								"</div>" +
							"</div>";
			}else{
				var html ="<div class='userliu cm-item ' >" +
								"<div class='toux'><img src='"+chat.headimgurl+"'></div>" +
								"<div class='nameping'>" +
								"<span>"+chat.nickname+"：<span class='sppin'>"+chat.comment+"</span></span>"+
							"</div>" +
						"</div>";
			}
			animat(html);
			seeIndex++;
		}else{
			stopMove();
		}
	}, 1000);
};
function stopMove(){
	clearInterval(itemScale);
}

//主要防止高度混乱
function animat(html){
	if(is_animat){
		is_animat = false;
		cmchat.append(html);
		var h = $('.cm-list').height();
		var length = $(".cm-item").length;
		itemHeight = $('.cm-item').eq(length-1).height()+10;
		if(!first){
			h = h + itemHeight;
		}else{
			first = false;
		}
		cmchat.animate({height:h+"px"},600,function(){
			is_animat = true;
		});
	}else{
		setTimeout(function(){
			animat(html);
		},300);
	}
}

//添加评论
function addCmItem(){
	//发评论
		if(isSend){
			return;
		}
		isSend =true;
		var text = $("#cmText").val();
		if(text==null||text==""||text==undefined){
			warn("请写下你的精彩评论~");
			isSend = false;
			return false;
		}
		var commentTimestamp  = Date.parse(new Date());//控制多次评论
		if((commentTimestamp-now)/1000<60){
				if(number>=commentMinuteMax){
					warn("评论太多次啦~");
					isSend = false;
					return false;
				}
		}else{
			now = commentTimestamp;
			number = 0;//重新计算
		}
		var url = "/ws/index.php?g=mobile&m=message&a=saveComment"
		var data = {
			"text":text,
			"wxid":wxid
		};
		$.post(url,data,function(data){
			isSend =false;
			if(data.error==0){
				number++;
				$("#cmText").val("");
				$("#userIntegral").html(data.integral);
				var html ="<div class='userliu cm-item ' >" +
							"<div class='toux'><img src='/ws/Mobile/Webroot/gf/images/g4logo.png'></div>" +
							"<div class='nameping'>" +
							"<span>城市客厅用户：<span class='sppin'>"+text+"</span></span>"+
						"</div>" +
					"</div>"
				animat(html);
			}else if(data.error=="4004"){
				warn("您已经被管理员禁言了~");
			}else if(data.error=="2002"){
				warn("您的吐槽有敏感词汇，无法发送~");
			}else{
				warn("评论失败~");
			}
		},"json");
}
function getBLen(str) {
  if (str == null) return 0;
  if (typeof str != "string"){
    str += "";
  }
  return str.replace(/[^\x00-\xff]/g,"00").length;
}


//不打断alert
function warn(msg,opt,left,top){
	if(opt){
	 	var obj = $("#"+opt);
	}
	new Toast({context:$('body'),message:msg},obj,left,top).show();
}
var Toast = function(config,obj,left,top){
	this.context = config.context==null?$('body'):config.context;//上下文
	this.message = config.message;//显示内容
	this.time = config.time==null?5000:config.time;//持续时间
	this.left = config.left;//距容器左边的距离
	this.top = ($(document).height()/4)*3;//距容器上方的距离
	if(obj){ 
	this.left = obj.offset().left + left;
	this.top = obj.offset().top + top;
	}
	this.init();
}
var msgEntity;
Toast.prototype = {
	//初始化显示的位置内容等
	init : function(){
		$("#toastMessage").remove();
		//设置消息体
		var msgDIV = new Array();
		msgDIV.push('<div id="toastMessage">');
		msgDIV.push('<span>'+this.message+'</span>');
		msgDIV.push('</div>');
		msgEntity = $(msgDIV.join('')).appendTo(this.context);
		//设置消息样式
		var left = this.left == null ? this.context.width()/2-17-msgEntity.find('span').width()/2 : this.left;
		var top = this.top == null ? '20px' : this.top;
		msgEntity.css({position:'absolute',top:top,'z-index':'9999',left:left,'background-color':'#2a70e6',color:'white','font-size':'14px',padding:'12px',margin:'5px','border-radius':'4px','-moz-border-radius':'4px','-webkit-border-radius':'4px','font-family':'微软雅黑'});
		//msgEntity.addClass(".toast");
		msgEntity.hide();
	},
	//显示动画
	show :function(){
		msgEntity.fadeIn(this.time/2);
		msgEntity.fadeOut(this.time/2);
	}
}